package com.zmj.sy.mom.srv.aps.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.Group;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.ShiftType;
import com.zmj.sy.mom.srv.aps.bean.vo.base.*;
import com.zmj.sy.mom.srv.aps.bean.vo.shifttype.*;
import com.zmj.sy.mom.srv.aps.mapper.GroupMapper;
import com.zmj.sy.mom.srv.aps.mapper.ShiftTypeMapper;
import com.zmj.sy.mom.srv.aps.utils.MkBeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class ShiftTypeService {

    private final ShiftTypeMapper shiftTypeMapper;
    private final GroupMapper groupMapper;

    @Transactional
    public void add(ShiftTypeAddReqVo reqVo) {
        ShiftType shiftType = MkBeanUtils.copyProperties(reqVo, ShiftType.class);
        shiftTypeMapper.insert(shiftType);
    }


    public BasePageResVo<ShiftTypeListResVo> listOld(ShiftTypeListReqVo reqVo) {
        LambdaQueryWrapper<ShiftType> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.hasText(reqVo.getName()), ShiftType::getName, reqVo.getName());
        Page<ShiftType> shiftTypePage = shiftTypeMapper.selectPage(reqVo.toPage(), wrapper);
        return BasePageResVo.of(shiftTypePage, ShiftTypeListResVo.class);
    }

    public BaseListResVo<ShiftTypeListResVo> list(ShiftTypeListReqVo reqVo) {
        LambdaQueryWrapper<ShiftType> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.hasText(reqVo.getName()), ShiftType::getName, reqVo.getName());
        List<ShiftType> shiftTypePage = shiftTypeMapper.selectList(wrapper);
        return BaseListResVo.of(shiftTypePage, ShiftTypeListResVo.class);
    }

    @Transactional
    public void edit(ShiftTypeEditReqVo reqVo) {
        ShiftType shiftType = MkBeanUtils.copyProperties(reqVo, ShiftType.class);
        shiftTypeMapper.updateById(shiftType);
    }

    @Transactional
    public void delete(BaseIdReqVo reqVo) {
        shiftTypeMapper.deleteById(reqVo.getId());
    }

    public BaseSelectResVo select() {
        List<ShiftType> shiftTypes = shiftTypeMapper.selectList(null);
        List<BaseSelectItemResVo> collect = shiftTypes.stream()
                .map(e -> new BaseSelectItemResVo(e.getName(), e.getId()))
                .collect(Collectors.toList());
        return BaseSelectResVo.of(collect);
    }

    @Transactional(readOnly = true)
    public ShiftTypeGetResVo get(BaseIdReqVo reqVo) {
        Group group = groupMapper.selectById(reqVo.getId());
        ShiftType shiftType = shiftTypeMapper.selectById(group.getShiftTypeId());
        return MkBeanUtils.copyProperties(shiftType, ShiftTypeGetResVo.class);
    }
}
